package com.ftg.elvis.controller;

import com.ftg.elvis.Util.CpachaUtil;
import com.ftg.elvis.pojo.Brand;
import com.ftg.elvis.pojo.Role;
import com.ftg.elvis.pojo.User;
import com.ftg.elvis.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理类
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class AdminUserController {

	@Autowired
	AdminUserService adminUserService;

	/**
	 * 注册
	 * @param user
	 * @return
	 */
	@PostMapping("/register")
	public int register(User user){

		//查询该用户名是否存在
		User is_exis = adminUserService.findUserByUsername(user);

		if(is_exis != null){
			//存在
			return 0;
		}else{
			//不存在，进行注册
			int data = adminUserService.register(user);
			return 1;
		}
	}

	/**
	 * 生成验证码
	 * @param req
	 * @param res
	 */
	@GetMapping("/getCpacha")
	public void getCpacha(HttpServletRequest req, HttpServletResponse res){
		CpachaUtil cpachaUtil = new CpachaUtil(4,117,36);
		String generatorVCode = cpachaUtil.generatorVCode();
		req.getSession().setAttribute("login", generatorVCode);
		BufferedImage bufferedImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
		try {
			ImageIO.write(bufferedImage, "gif", res.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 登录
	 * @param user
	 * @param req
	 * @param code
	 * @return
	 */
	@PostMapping("/login")
	public int login(User user, String code, HttpServletRequest req){

		String login_code = (String) req.getSession().getAttribute("login");
		if(login_code == null){
			return 4;//会话超时，请刷新页面
		}
		if(!code.toUpperCase().equals(login_code.toUpperCase())){
			return 5;//验证码错误，请重新输入
		}else{
			User login_user = adminUserService.login(user);
			if(login_user != null){
				//用户登陆成功
				//保持登录状态
				req.getSession().setAttribute("user", login_user);
				return 1;
			}else{
				//用户名不存在
				if(adminUserService.findUserByUsername(user) == null){
					return 2;
				}else{
					//密码错误
					return 3;
				}
			}
		}
	}

	@PostMapping("/add")
	public int addUser(User user){
		return adminUserService.addUser(user);
	}

	@PostMapping("/update")
	public int updateUser(User user){
		return adminUserService.updateUser(user);
	}

	@DeleteMapping("/del/{id}")
	public int deleteUser(@PathVariable String id){
		return adminUserService.deleteUser(id);
	}

	@GetMapping("/showAll")
	public Map<String, Object> showAll(int page, int limit, String username){
		Map<String,Object> map = new HashMap<>();
		List<User> list = adminUserService.showAll(page,limit,username);
		map.put("data", list);//11 10
		map.put("username", username);//11 10
		map.put("code", 0);
		map.put("count", adminUserService.getCount(username));
		return map;
	}

	/**
	 * 显示所有的角色
	 * @return
	 */
	@GetMapping("/showAllRole")
	public Map<String, Object> showAllRole(){
		Map<String,Object> map = new HashMap<>();
		List<Role> allRole = adminUserService.showAllRole();
		map.put("data", allRole);
		map.put("code", 0);
		return map;
	}

	/**
	 * 显示我的所有角色
	 * @param uid
	 * @return
	 */
	@GetMapping("/showMyRole")
	public Map<String, Object> showMyRole(String uid){
		Map<String,Object> map = new HashMap<>();
		List<Role> myRole = adminUserService.showMyRole(uid);
		List<Role> allRole = adminUserService.showAllRole();
		map.put("data1", allRole);
		map.put("data2", myRole);
		map.put("code", 0);
		return map;
	}

	/**
	 * 显示我没有的角色
	 * @param uid
	 * @return
	 */
	@GetMapping("/showNoRole")
	public Map<String, Object> showNoRole(String uid){
		Map<String,Object> map = new HashMap<>();
		List<Role> myRole = adminUserService.showNoRole(uid);
		map.put("data", myRole);
		map.put("code", 0);
		return map;
	}

	/**
	 * 修改某个用户的角色
	 * @param user_id
	 * @param role_ids
	 * @return
	 */
	@GetMapping("/updateRoles/{user_id}/{role_ids}")
	public int updateRoles(@PathVariable String user_id, @PathVariable String[] role_ids){
		return adminUserService.updateRoles(user_id, role_ids);
	}

	@GetMapping("/showBrand")
	public Map<String, Object> showBrand(){
		Map<String, Object> map = new HashMap<>();
		List<Brand> list = adminUserService.showBrand();
		map.put("data", list);
		map.put("code", 0);
		return map;
	}
}
